//
//  videoDB.m
//  FFmpeg_X264_Codec
//
//  Created by 陆永亨 on 16/1/8.
//  Copyright © 2016年 suntongmian@163.com. All rights reserved.
//

#import "videoDB.h"
static NSString * TABLENAME = @"DBVideo";
static NSString * videoId = @"id";
static NSString * videoPath = @"path";

@implementation videoDB


- (BOOL) isOSWDPTableOK:(FMDatabase * )database
{
    FMResultSet *rs = [database executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", TABLENAME];
    if (!rs) {
        [rs close];
        return NO;
    }
    while ([rs next])
    {
        // just print out what we've got in a number of formats.
        NSInteger count = [rs intForColumn:@"count"];
        NSLog(@"isTableOK %ld", count);
        
        if (0 == count)
        {
            [rs close];
            return NO;
        }
        else
        {
            [rs close];
            return YES;
        }
    }
    [rs close];
    return NO;
}

-(void)CreatVideoTableWith:(FMDatabase *)db1
{
    
    //[self creatQxDbFiled:users];
    if ([self isOSWDPTableOK:db1] ==YES) {
        return;
    }
    if ([db1 open]) {
        NSString *sqlCreateTable =  [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' TEXT default '','%@' TEXT default '')",TABLENAME,videoId,videoPath];
        BOOL res = [db1 executeUpdate:sqlCreateTable];
        if (!res) {
            NSLog(@"error when creating db table");
        } else {
            NSLog(@"success to creating dbCollaboration table");
        }
        // [_db close];
        
    }
    
}

-(void)InsertGoodTWithFMDB:(FMDatabase*)db1 videoPath:(NSString*)videoPath1
{
    if ([db1 open]) {
        
        NSString * sqlUpdateTable =nil;
        
        // if (isSeleted ==NO) {
        sqlUpdateTable = [NSString stringWithFormat:@"INSERT INTO '%@' ('%@','%@') VALUES ('%@','%@')",TABLENAME,videoId,videoPath,@"",videoPath1];
        //        }
        //        else
        //        {
        //            sqlUpdateTable = [NSString stringWithFormat:@"UPDATE '%@' SET blogId ='%@',id = '%@',userId = '%@',userName = '%@',targeUserId = '%@' where blogId = '%@'",TABLENAME,blogid,goodMM.goodId,goodMM.userId,goodMM.userName,goodMM.targeUserId,blogid];
        //        }
        
        
        
        
        
        
        
        BOOL res = [db1 executeUpdate:sqlUpdateTable];
        
        
        if (!res)
        {
            NSLog(@"error when updatecoll db table");
        } else
        {
            NSLog(@"success to updatecoll db table");
        }
        
        
        
        
        
    }
}

-(NSMutableArray *)selectAllVideowWithFMDBdatabase:(FMDatabase *)db1
{
    NSMutableArray * spaceDataArray = [NSMutableArray array];
    if ([db1 open]) {
        
        NSString * sql =nil;
        
        sql = [NSString stringWithFormat:
               @"select * from '%@'",TABLENAME];
        
        
        
        FMResultSet * rs = [db1 executeQuery:sql];
        
        
        if (rs) {
            while ([rs next]) {
                
                NSString * videoPath = [rs stringForColumn:@"path"];
                
                [spaceDataArray addObject:videoPath];
            }
        }
        
        [rs close];
    }
    
    return spaceDataArray;
}
-(void)deleteVideoWithFMDBdatabase:(FMDatabase*)db1 path:(NSString*)path
{
    
    if ([db1 open]) {
        NSString * sql = [NSString stringWithFormat:
                          @"DELETE FROM %@ where path = '%@'",TABLENAME,path];
        
        BOOL a =  [db1 executeUpdate:sql];
        if (a == YES) {
            NSLog(@"删除成功");
        }
        else
            NSLog(@"删除失败");
        
    }
}
@end
